﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;

namespace Yuanlin.Business
{
    public class CompanyCardEditControl : YuanlinUserControl,
        IReplyConverter<CompanyCardReply, GetOperation>
    {
        protected ValuedTextBox companyNameValuedTextBox;
        protected ValuedTextBox companyAddressValuedTextBox;
        protected ValuedTextBox companyContactPhoneValuedTextBox;
        protected ValuedTextBox companyPostCodeValuedTextBox;
        protected ValuedTextBox companyFaxNumberValuedTextBox;
        protected ValuedTextBox companyEmailValuedTextBox;
        protected ValuedTextBox companySiteValuedTextBox;
        protected ValuedTextBox companyQQValuedTextBox;
        protected ValuedTextBox companyMSNValuedTextBox;
        protected ValuedTextBox companyScaleValuedTextBox;
        protected ValuedTextBox companyTelephone2ValuedTextBox;
        protected ValuedTextBox companyTelephone1ValuedTextBox;
        protected CheckBoxList companyTypesCheckBoxList;
        protected ValuedDropDownList provinceDropDownList;
        protected ValuedDropDownList cityDropDownList;
        protected ValuedDropDownList townDropDownList;
        protected ValuedDropDownList smallTownDropDownList;
        protected ValuedEditor descriptionValuedEditor;
        protected Literal companyCardLiteral;
        protected IButton SaveButton;
        protected IButton BackButton;
        private CompanyCard companyCard;

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            requestBinders.Add(RequestHelper.BuildGetCompanyCardRequest(this));
        }
        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack && !AjaxManager.IsCallBack)
            {
                Bind();
            }
            SaveButton.Click += new EventHandler(SaveButton_Click);
            BackButton.Click += new EventHandler(BackButton_Click);
            AjaxManager.Register(this, "AjaxManager");
        }

        private void Bind()
        {
            if (companyCard == null)
            {
                return;
            }

            this.companyNameValuedTextBox.Value = companyCard.CompanyName.Value;
            this.companyAddressValuedTextBox.Value = companyCard.CompanyAddress.Value;
            this.companyContactPhoneValuedTextBox.Value = companyCard.CompanyContactPhone.Value;
            this.companyPostCodeValuedTextBox.Value = companyCard.CompanyPostCode.Value;
            this.companyFaxNumberValuedTextBox.Value = companyCard.CompanyFaxNumber.Value;
            this.companyEmailValuedTextBox.Value = companyCard.CompanyEmail.Value;
            this.companySiteValuedTextBox.Value = companyCard.CompanySite.Value;
            this.companyQQValuedTextBox.Value = companyCard.CompanyQQ.Value;
            this.companyMSNValuedTextBox.Value = companyCard.CompanyMSN.Value;
            this.descriptionValuedEditor.Value = companyCard.CompanyDescription.Value;
            companyScaleValuedTextBox.Value = companyCard.CompanyScale.Value;
            companyTelephone1ValuedTextBox.Value = companyCard.CompanyFixedTelephone1.Value;
            companyTelephone2ValuedTextBox.Value = companyCard.CompanyFixedTelephone2.Value;
            EntityList companys = BusinessManager.GetCompanyTypeList(RequestHelper.BuildGetCompanyTypeListRequest());
            if (companys != null)
            {
                BindCheckBoxList(companyTypesCheckBoxList, companys, "EntityId", "Title", companyCard.CompanyTypes.Value.Trim(','));
            }
            AreaListRequest request = new AreaListRequest();
            request.UpAreaId = 0;
            EntityList areas = BusinessManager.GetAreaList(RequestHelper.BuildGetAreaListRequest(request));
            if (areas == null)
                return;
            BindDropDownList(provinceDropDownList, areas, "EntityId", "Title", companyCard.Province.Value.ToString());
            if (this.provinceDropDownList.SelectedValue.Length == 0)
                return;

            request = new AreaListRequest();
            request.UpAreaId = Convert.ToInt32(this.provinceDropDownList.SelectedValue);
            areas = BusinessManager.GetAreaList(RequestHelper.BuildGetAreaListRequest(request));
            if (areas == null)
                return;
            BindDropDownList(cityDropDownList, areas, "EntityId", "Title", companyCard.City.Value.ToString());
            if (this.cityDropDownList.SelectedValue.Length == 0)
                return;

            request = new AreaListRequest();
            request.UpAreaId = Convert.ToInt32(this.cityDropDownList.SelectedValue);
            areas = BusinessManager.GetAreaList(RequestHelper.BuildGetAreaListRequest(request));
            if (areas == null)
                return;
            BindDropDownList(townDropDownList, areas, "EntityId", "Title", companyCard.Town.Value.ToString());
            if (this.townDropDownList.SelectedValue.Length == 0)
                return;

            request = new AreaListRequest();
            request.UpAreaId = Convert.ToInt32(this.townDropDownList.SelectedValue);
            areas = BusinessManager.GetAreaList(RequestHelper.BuildGetAreaListRequest(request));
            if (areas == null)
                return;
            BindDropDownList(smallTownDropDownList, areas, "EntityId", "Title", companyCard.SmallTown.Value.ToString());

            if (companyCard.IsRegisterCompany.Value == 1)
            {
                SaveButton.Visible = false;
                companySiteValuedTextBox.Enabled = false;
                companyCardLiteral.Text = ResourceManager.GetString("System_Applications_Yuanlin_CompanyCard_View", "ControlPanelResources.xml");
            }
            else
            {
                companyCardLiteral.Text = ResourceManager.GetString("System_Applications_Yuanlin_EditCompanyCard", "ControlPanelResources.xml");
            }
        }

        private void BindDropDownList(ValuedDropDownList ddl, EntityList list, string valueField, string textField, string selectedValue)
        {
            ddl.DataSource = list;
            ddl.DataValueField = valueField;
            ddl.DataTextField = textField;
            ddl.DataBind();
            if (selectedValue.Length > 0)
            {
                ddl.Value = selectedValue;
            }
        }
        private void BindCheckBoxList(CheckBoxList cbl, EntityList list, string valueField, string textField, string selectedValue)
        {
            cbl.DataSource = list;
            cbl.DataValueField = valueField;
            cbl.DataTextField = textField;
            cbl.DataBind();

            if (selectedValue == null)
                return;

            string[] strArray = selectedValue.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            if (strArray.Length == 0)
                return;

            List<string> selectedList = new List<string>(strArray.Length);
            foreach (string itemStr in strArray)
            {
                selectedList.Add(itemStr);
            }

            foreach (ListItem item in cbl.Items)
            {
                if (selectedList.Contains(item.Value))
                {
                    item.Selected = true;
                }
            }
        }

        public void BackButton_Click(object sender, EventArgs e)
        {
            PageRedirect();
        }

        public void SaveButton_Click(object sender, EventArgs e)
        {
            if (companyCard != null)
            {
                companyCard.CompanyName.Value = CensorWordManager.CensorWord(this.companyNameValuedTextBox.Value);
                companyCard.CompanyAddress.Value = CensorWordManager.CensorWord(this.companyAddressValuedTextBox.Value);
                companyCard.CompanyContactPhone.Value = this.companyContactPhoneValuedTextBox.Value;
                companyCard.CompanyPostCode.Value = this.companyPostCodeValuedTextBox.Value;
                companyCard.CompanyFaxNumber.Value = this.companyFaxNumberValuedTextBox.Value;
                companyCard.CompanyEmail.Value = this.companyEmailValuedTextBox.Value;
                companyCard.CompanySite.Value = this.companySiteValuedTextBox.Value;
                companyCard.CompanyQQ.Value = this.companyQQValuedTextBox.Value;
                companyCard.CompanyMSN.Value = this.companyMSNValuedTextBox.Value;
                companyCard.CompanyDescription.Value = CensorWordManager.CensorWord(this.descriptionValuedEditor.Value);
                companyCard.CompanyScale.Value = CensorWordManager.CensorWord(companyScaleValuedTextBox.Value);
                companyCard.CompanyFixedTelephone1.Value = companyTelephone1ValuedTextBox.Value;
                companyCard.CompanyFixedTelephone2.Value = companyTelephone2ValuedTextBox.Value;

                string companyTypes = string.Empty;
                foreach (ListItem item in companyTypesCheckBoxList.Items)
                {
                    if (item.Selected)
                    {
                        companyTypes += "," + item.Value;
                    }
                }
                if (companyTypes.Length > 0)
                {
                    companyTypes = companyTypes + ",";
                }
                companyCard.CompanyTypes.Value = companyTypes;

                companyCard.Province.Value = Convert.ToInt32(provinceDropDownList.Value);
                companyCard.City.Value = Convert.ToInt32(cityDropDownList.Value);
                companyCard.Town.Value = Convert.ToInt32(townDropDownList.Value);
                companyCard.SmallTown.Value = Convert.ToInt32(smallTownDropDownList.Value);

                if (BusinessManager.UpdateCompanyCard(companyCard) == -2)
                {
                    Globals.ShowMessage(this.Page, "修改失败", "此信息已经存在！");
                    return;
                }
            }
            PageRedirect();
        }
        private void PageRedirect()
        {
            Page.Response.Redirect(SiteUrls.Instance().GetCompanyCardListUrl());
        }
        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public string GetChildCategories(int parentCategoryID)
        {
            if (parentCategoryID == -1)
            {
                return string.Empty;
            }
            AreaListRequest request = new AreaListRequest();
            request.UpAreaId = parentCategoryID;
            EntityList areas = BusinessManager.GetAreaList(RequestHelper.BuildGetAreaListRequest(request));

            if (areas != null && areas.Count > 0)
            {
                string areaFormat = "{0}:{1}";
                string areaString = string.Empty;
                foreach (Area area in areas)
                {
                    areaString += ";" + string.Format(areaFormat, area.EntityId.Value.ToString(), area.Title.Value);
                }

                if (areaString.Length > 0)
                {
                    areaString = areaString.Substring(1);
                }
                return areaString;
            }

            return string.Empty;
        }

        #region IReplyConverter<CompanyCardReply,GetOperation> Members

        public void ConvertReply(CompanyCardReply reply, GetOperation operation)
        {
            if (reply != null)
            {
                companyCard = reply.Entity as CompanyCard;

                if (!Page.IsPostBack)
                {
                    Bind();
                }
            }
        }

        #endregion
    }
}